﻿
Namespace Processing
    Public Class DeleteWebSiteCache
        Inherits BaseProcess(Of DeleteWebSiteCache)
        Implements Interfaces.IProcess


        Public Sub Action() Implements Interfaces.IProcess.Action
            CleanWebCache()
        End Sub

        Public ReadOnly Property Name As String Implements Interfaces.IProcess.Name
            Get
                Return "Cleaning Web Project Cache"
            End Get
        End Property

        Private Shared Sub CleanWebCache()
            Dim roortWebCacheDirectory As String
            Dim userDirectory As String
            Dim myDocuments As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments
            If (Environment.OSVersion.Version.Major >= 5) Then
                roortWebCacheDirectory = "{0}:\Users\{1}\AppData\Local\Microsoft\WebsiteCache"
                userDirectory = myDocuments.Remove(0, myDocuments.LastIndexOf("Users\")).Replace("\Documents", "").Replace("Users\", "")
            Else
                roortWebCacheDirectory = "{0}:\Documents and Settings\{1}\Local Settings\Application Data\Microsoft\WebsiteCache"
                userDirectory = myDocuments.Remove(0, myDocuments.LastIndexOf("Documents and Settings\")).Replace("\My Documents", "").Replace("Documents and Settings\", "")
            End If
            Dim systemDrive As String = Environment.SystemDirectory.Substring(0, 1)
            Dim rootDir As String = String.Format(roortWebCacheDirectory, systemDrive, userDirectory)

            For x As Integer = 0 To 3
                DeleteWebSiteCache.DeleteDirectoryPass(rootDir)
            Next
        End Sub

        Private Shared Sub DeleteDirectoryPass(ByVal rootDir As String)
            If My.Computer.FileSystem.DirectoryExists(rootDir) Then
                My.Computer.FileSystem.GetDirectories(rootDir).AsParallel.ForAll(Sub(deldir)
                                                                                     Try
                                                                                         My.Computer.FileSystem.DeleteDirectory(deldir, FileIO.DeleteDirectoryOption.DeleteAllContents)
                                                                                     Catch ex As Exception
                                                                                         'Do nothing just skip
                                                                                     End Try
                                                                                 End Sub)
            End If
        End Sub

    End Class
End Namespace

